Ext.define('HIT.controller.DealSendMessage', {
	extend : 'Ext.app.Controller',
	models : [ 'DealSendMessage' ],
	stores : [ 'DealSendMessage' ],
	views : [ 'DealSendMessageGrid','DealSendSearchWin' ],
	onButtonClick : function(button, e, options) {
		var tabpanelId = 'tab-deal-send';
		var tab = Ext.getCmp(tabpanelId);
		if (tab == undefined) {
			return;
		}
		if (button.id == 'deal-express-send') {
			
			var grid = Ext.getCmp('tab-deal-send');
			if (grid == undefined) {
				return;
			}
			// 确认一览记录选中情况
			var selModel = grid.getSelectionModel();
			// 有记录被选中
			if (selModel.hasSelection()) {
				// 选中了多条记录
				var tab = Ext.getCmp('tab-deal-send');
				var model = selModel.getSelection();
				// 当没有快递单号或没有收件人时不允许发送邮件或短信
				for (var i = 0; i < model.length; i++) {
					var express = model[i].get('exp_no');
					var exp_rec = model[i].get('exp_rec');
					if (express == null || express == '' || exp_rec == null || exp_rec == '') {
						Share.ShowMessage('I0074');
						return;
					}
				}
				// 单据号
				var doc_no = [];
				
				for(var i=0;i<model.length;i++){
					doc_no.push(model[i].get('doc_no'));
                }
				Share.AjaxRequest({
					url : ctx + "/SendMessage",
					params : {
						doc_no : doc_no.join(",")
					},
					showMsg : false,
					callback : function(json) {
						if (json.resultcode == '0') {
							if (json.expressCode == '0' && json.docStatus == '0') {
								Share.ShowMessage('I0006','发送');
								tab.getStore().reload();
							} else if (json.expressCode == '0' && json.docStatus == '1') {
								Ext.Msg.alert('错误', '单据号' + json.docNo + '短信发送失败！');
								tab.getStore().reload();
							} else if (json.expressCode == '1' && json.docStatus == '0') {
								
							}
							Share.ShowMessage('I0006','发送');
							tab.getStore().reload();
						} else if (json.resultcode == '1') {
							Ext.Msg.alert('提示','未启用寄送功能，请在配置中心设置模板。');
						} else {
							Share.ShowMessage('I0007','发送');
						}
					}
				});
			} else {
				Share.ShowMessage('I0008');
				return;
			}
		}else if(button.id == 'deal-send-search'){
        	var s_win = Ext.create('widget.DealSendSearchWin');
        	Share.showWindow(s_win,true);  
        } else if(button.id == 'send_search_button'){
        	var tab = Ext.getCmp('tab-deal-send');
        	var startno = Ext.ComponentMgr.get("send_search_sno").getValue() == null ? "":Ext.ComponentMgr.get("send_search_sno").getValue();
      		var endno = Ext.ComponentMgr.get("send_search_eno").getValue()== null ? "":Ext.ComponentMgr.get("send_search_eno").getValue();
      		var nos = Ext.ComponentMgr.get("send_search_nos").getValue().toString()== null ? "":Ext.ComponentMgr.get("send_search_nos").getValue().toString();
      		var sexpressno = Ext.ComponentMgr.get("send_search_sexpressno").getValue()== null ? "":Ext.ComponentMgr.get("send_search_sexpressno").getValue();
      		var eexpressno = Ext.ComponentMgr.get("send_search_eexpressno").getValue()== null ? "":Ext.ComponentMgr.get("send_search_eexpressno").getValue();
      		var startday = Ext.ComponentMgr.get("send_search_sday").getValue()== null ? "":Ext.ComponentMgr.get("send_search_sday").getValue();
      		var endday = Ext.ComponentMgr.get("send_search_eday").getValue()== null ? "":Ext.ComponentMgr.get("send_search_eday").getValue();     
      		var custom = Ext.ComponentMgr.get("send_search_cust").getValue()==null ? "":Ext.ComponentMgr.get("send_search_cust").getValue();   
      		var store = tab.getStore();
      		store.currentPage = 1;
      		store.removeAll();
            store.on('beforeload', function(store) {
            	Ext.apply(store.proxy.extraParams , 
            		{startno:(startno==""?null:startno),
            		endno:(endno==""?null:endno),
            		nos:(nos==""?null:nos),
            		sexpressno:(sexpressno==""?null:sexpressno),
            		eexpressno:(eexpressno==""?null:eexpressno),
            		startday:(startday==""?null:startday),
            		endday:(endday==""?null:endday),
            		custom:(custom==""?null:custom)});
            	});
            store.load(); 
            button.up('window').close();
        }
	},
	init : function(application) {
		this.control({
			"button" : {
				click : this.onButtonClick
			}
		});
	}
});
